package com.ssx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ssx.entity.Customer;
import com.ssx.entity.Customershare;
import com.ssx.entity.Customervisit;
import com.ssx.entity.Employees;
import com.ssx.mapper.CustomerMapper;
import com.ssx.mapper.CustomershareMapper;
import com.ssx.mapper.EmployeesMapper;
import com.ssx.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ssx
 * @since 2020-10-24
 */
@Service
@Transactional
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomershareMapper customershareMapper;
    @Autowired
    private EmployeesMapper employeesMapper;

    public int delOneCus(int id,int empid) {
        int delete=0;
        //有所有权
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().eq("customer.empid", empid).eq("cusId", id));
        //经理或管理员
        Employees employees = employeesMapper.selectOne(new QueryWrapper<Employees>().eq("employees.empid", empid).eq("employees.roleId", 3));
        if (customer!=null||employees==null){
            customershareMapper.delete(new QueryWrapper<Customershare>().eq("cusId",id));
            delete= customerMapper.deleteById(id);
        }

        return delete;
    }

    @Override
    public List<Customer> listFromEmp(int start, int pagesize, String search, int empid) {
        return customerMapper.listFromEmp(new QueryWrapper<Customer>().eq("customer.empid",empid)
                .and(i->i.like("cusName", search).or().like("address", search).or().like("contact", search)
                        .or().like("name",search)).last(" limit " + start + "," + pagesize));
    }

    @Override
    public int countFromEmp(String search, int empid) {
        return customerMapper.listFromEmp(new QueryWrapper<Customer>().eq("customer.empid",empid)
                .and(i->i.like("cusName", search).or().like("address", search).or().like("contact", search)
                        .or().like("name",search))).size();
    }

    @Override
    public List<Customer> listFromManger(int start, int pagesize, String search) {
        return customerMapper.listFromManger(new QueryWrapper<Customer>().like("cusName", search)
                .or().like("address", search).or().like("contact", search)
                .or().like("name",search).last(" limit " + start + "," + pagesize));
    }

    @Override
    public int countFromManger(String search) {
        return customerMapper.listFromManger(new QueryWrapper<Customer>().like("cusName", search)
                .or().like("address", search).or().like("contact", search)
                .or().like("name",search)).size();

    }

    public  Customer showOneCus(int id){
        return customerMapper.selectById(id);
    }

    @Override
    public boolean save(Customer entity) {
        Employees employees = new Employees();
        employees.setEmpid(entity.getEmployees().getEmpid());
        Customershare customershare = customershareMapper.selectOne(new QueryWrapper<Customershare>().eq("empid", entity.getEmployees().getEmpid()).eq("cusId", entity.getCusId()));
        if (customershare==null){
            if (customerMapper.insert(entity)>0){
                Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().eq("cusName", entity.getCusName()));
                return customershareMapper.insert(new Customershare(0,customer,employees))>0;
            }else {
                return false;
            }

        }
        return false;
    }
}
